﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Business.Entity;
using System.Data.SqlClient;
using System.Data;

namespace Business.Dal
{  
    public class UlkeProvider : Provider<Ulke>
 {       
        public override bool Insert(Ulke tObject)
        {
            bool returnValue = false;
            
            SqlCommand cmd = new SqlCommand("UlkeEkle");
            cmd.Parameters.Add("@UlkeID", SqlDbType.Int).Value = tObject.UlkeID;
            cmd.Parameters.Add("@UlkeAdi", SqlDbType.NVarChar).Value = tObject.UlkeAdi;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            cmd.ExecuteNonQuery();
            returnValue = true;
            
            return returnValue;
            /// test
        }

        public override bool Update(Ulke tObject)
        {
            bool returnValue = false;

            SqlCommand cmd = new SqlCommand("UlkeGuncelle");
            cmd.Parameters.Add("@UlkeID", SqlDbType.Int).Value = tObject.UlkeID;
            cmd.Parameters.Add("@UlkeAdi", SqlDbType.NVarChar).Value = tObject.UlkeAdi;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());

            return returnValue;
        }

        public override bool Delete(int RecordID)
        {
            bool returnValue = false;

            SqlCommand cmd = new SqlCommand("UlkeSil");
            cmd.Parameters.Add("@UlkeID", SqlDbType.Int).Value = RecordID;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            Tools.Open();
            returnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());

            return returnValue;
        }

        public List<Ulke> GetAllRows()
        {
            
            List<Ulke> returnValue = new List<Ulke>();

            SqlCommand cmd = new SqlCommand("UlkelerGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            SqlDataReader dr;
            Tools.Open();
            dr = cmd.ExecuteReader();

            //dr.Read();

            while (dr.Read())
            {
                Ulke p = new Ulke();
                // veritananından null gelebilecek kolonlar için bir if yzildi int değer null gelirse geriye 0 değerini döndürmesi sağlandı 
                p.UlkeAdi = dr["UlkeAdi"] is DBNull ? String.Empty : dr["UlkeAdi"].ToString();
                p.UlkeID = dr["UlkeID"] is DBNull ? 0 : Convert.ToInt32(dr["UlkeID"]);

                returnValue.Add(p);
            }

            dr.Dispose();

            return returnValue;

        }

        public Ulke GetUlke(int RecordID)
        {


            SqlCommand cmd = new SqlCommand("UlkelerGetir");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Connection = Tools.Connection();
            SqlDataReader dr;
            Tools.Open();
            dr = cmd.ExecuteReader();

            dr.Read();

            Ulke p = new Ulke();
            // veritananından null gelebilecek kolonlar için bir if yzildi int değer null gelirse geriye 0 değerini döndürmesi sağlandı 
            p.UlkeAdi = dr["UlkeAdi"] is DBNull ? String.Empty : dr["UlkeAdi"].ToString();
            p.UlkeID = dr["UlkeID"] is DBNull ? 0 : Convert.ToInt32(dr["UlkeID"]);

            dr.Dispose();


            return p;
        }
    }
}
